package jmx ;

import java.lang.management.ManagementFactory ;
import java.rmi.registry.LocateRegistry ;
import java.rmi.registry.Registry ;

import javax.management.InstanceAlreadyExistsException ;
import javax.management.MBeanRegistrationException ;
import javax.management.MBeanServer ;
import javax.management.MBeanServerFactory ;
import javax.management.MalformedObjectNameException ;
import javax.management.NotCompliantMBeanException ;
import javax.management.ObjectName ;
import javax.management.remote.JMXConnectorServer ;
import javax.management.remote.JMXConnectorServerFactory ;
import javax.management.remote.JMXServiceURL ;

/**
 * @author 80002165 @date 2017年7月17日 下午8:44:22
 */
public class HelloAgent {
    public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
        MBeanServer server = MBeanServerFactory.createMBeanServer() ;
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer() ;
        
        String domainName = "MyMBean" ;
        
        ObjectName helloName = new ObjectName(domainName + ":name=HelloWorld") ;
        
        mbs.registerMBean(new Hello(), helloName) ;
        
        ObjectName adapterName = new ObjectName(domainName + ":name=htmladapter.port=8082") ;
        HtmlAdaptorServer adapter = new HtmlAdaptorServer() ;
        adapter.start() ;
        mbs.registerMBean(adapter, adapterName) ;
        
        int rmiPort = 1099 ;
        Registry registry = LocateRegistry.createRegistry(rmiPort) ;
        
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + domainName) ;
        JMXConnectorServer jmxConnector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs) ;
        jmxConnector.start() ;
    }
}
